package com.binguo.service;

import com.binguo.model.api.dto.OrderDTO;
import com.binguo.model.model.StatisticOrder;
import com.binguo.model.repository.StatisticOrderRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Condition;

import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @author yuritian
 * @Title: StatisticOrderService
 * @ProjectName binguoDevelop
 * @Description: TODO
 * @date 2019-06-2413:36
 */
@Service
public class StatisticOrderService {

    @Autowired
    StatisticOrderRepository statisticOrderRepository;

    private List<String> monthTimeEnds = Arrays.asList("/31 23:59:59", "/30 23:59:59", "/28 23:59:59", "/29 23:59:59");

    private String monthTimeStart = "/01 00:00:00";


    public void updateStatisticOrder(OrderDTO orderDTO){

        if(orderDTO.getCustomerId() != null){
            Calendar cale;
            cale = Calendar.getInstance();
            Integer year = cale.get(Calendar.YEAR);
            Integer month = cale.get(Calendar.MONTH) + 1;
            Integer day = day(year,month);
            String startDate = year + "/" + month + monthTimeStart;
            String endDate = year + "/" + month + monthTimeEnds.get(day);

            Condition condition = new Condition(StatisticOrder.class);
            condition.or().andEqualTo("shopId",orderDTO.getShopId())
                    .andEqualTo("customerId",orderDTO.getCustomerId())
                    .andBetween("statTime",startDate,endDate);
            List<StatisticOrder> statisticOrderList = statisticOrderRepository.findByCondition(condition);
            if (null == statisticOrderList || statisticOrderList.size() == 0){
                StatisticOrder statisticOrder = new StatisticOrder();
                statisticOrder.setShopId(orderDTO.getShopId());
                statisticOrder.setCustomerId(orderDTO.getCustomerId());
                statisticOrder.setStatTime(new Date(cale.getTimeInMillis()));
                statisticOrder.setMonthMoneyAmount(orderDTO.getRealFee());
                statisticOrder.setMonthConsumeCount(1);
                statisticOrderRepository.save(statisticOrder);
            }else {
                StatisticOrder statisticOrder = statisticOrderList.get(0);
                statisticOrder.setMonthConsumeCount(statisticOrder.getMonthConsumeCount()+1);
                statisticOrder.setMonthMoneyAmount(statisticOrder.getMonthMoneyAmount()+orderDTO.getRealFee());
                statisticOrderRepository.update(statisticOrder);
            }
        }


    }

    private Integer day(int year,int month){
        int sum = 0;
        if (month == 2){
            boolean isAleapyear = (year % 4 == 0 && year % 100 != 0) || year % 400 == 0;
            if (isAleapyear) {
                sum = 3;
            } else {
                sum = 2;
            }
        }else if (month%2 == 1){
            if (month<=7){
                sum = 1;
            } else {
                sum = 0;
            }
        } else {
            if (month > 7) {
                sum = 0;
            } else {
                sum = 1;
            }
        }
        return sum;
    }
}
